<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\captcha\Captcha;

class Navigation extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $list=db('navigation')->select();
        $this->assign('list',$list);
        return  $this->fetch();
    }
	//验证码
	public function add(){
        
        if( Request()->isPost() ){
            $inputdata = input();
            $data = [];
            $data["dname"] = $inputdata["dname"];
            $data["age"] = $inputdata["age"];
            $data["yname"] = $inputdata["yname"];
            $data["img"] = $inputdata["img"];
            $res = db( "navigation" )->insert( $data );
            if( $res ){
                $this->success( "添加成功", url( "navigation/index" ) );
            }else{
                $this->error( "添加失败" );
            }
        }else{
            return $this->fetch();
        }
    }
    public function save(){
        if( Request()->isPost() ){
            $inputdata = input();
            $data = [];
            $data["dname"] = $inputdata["dname"];
            $data["age"] = $inputdata["age"];
            $data["tiao"] = $inputdata["tiao"];
            $data["status"] = $inputdata["status"];
            $data["img"] = $inputdata["img"];
            $res = db( "navigation" )->insert( $data );
            if( $res ){
                $this->success( "添加成功", url( "navigation/index" ) );
            }else{
                $this->error( "添加失败" );
            }
        }else{
            return $this->fetch();
        }
    }
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('public/upload/');
        if ($info) {
            $path = '/public/upload/'.$info->getSaveName();
            return json(['code'=>0,'msg'=>'上传成功！','data'=>$path]);
        }else{
            return json(['code'=>1,'msg'=>'上传失败！','data'=>'']);
        }
    }
    public function update(){
        $field = 'id,dname,age,img,yname';
        $id=input('id');
        $data = db('navigation')->where('id',$id)->find();
        $this->assign('data',$data);
        if(request()->isPost())
        {
            $inputdata = input();
            $where['dname'] = $inputdata['dname'];
            $where['age'] = $inputdata['age'];
            $where['img'] = $inputdata['img'];
            $where['yname'] = $inputdata['yname'];
            $res = db( "navigation" )->field($field)->where('id',$inputdata['id'])->update( $inputdata );
            if( $res ){
                $this->success( "修改成功",url('navigation/index') );
            }else{
                $this->error( '修改失败' );
            }
        }return $this->fetch();
    }
    public function delete($id){
        $id = input('id');
        $res=db('navigation')->where('id',$id)->delete();
        if($res)
        {
            $this->success('删除成功','navigation/index');
        }else {
            $this->error('删除失败');
        }
    }

    //状态开启关闭
    public function state(){
        $status = input("status");
        $id = input("id");
        $where = array();
        $where["id"] = $id;
        $rst = db("navigation")->update(["status"=>$status]);
        if($rst){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
}
